﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Site_Tracker
{
    public partial class Main_Window : Form
    {
        public Dictionary<string, Dictionary<string, string>> KnownSiteDict;
        public Dictionary<string, Dictionary<string, string>> UnknownSiteDict;

        public Main_Window()
        {
            InitializeComponent();
        }

        private void Main_Form_Load(object sender, EventArgs e)
        {
            KnownSiteDict = new Dictionary<string, Dictionary<string, string>>();
            UnknownSiteDict = new Dictionary<string, Dictionary<string, string>>();
        }

        private void Process_Sites_click(object sender, EventArgs e)
        {
            UnknownSiteDict = new Dictionary<string, Dictionary<string, string>>();
            UnknownSitesChecklist.Items.Clear();
            KnownSitesChecklist.Items.Clear();

            string tRawText = SiteTextBox.Text;
            List<string> tUnsplitList = new List<string>();

            if (tRawText.Contains("\n"))
                tUnsplitList = tRawText.Split(new string[]{"\n"}, StringSplitOptions.RemoveEmptyEntries).ToList();
            else if (tRawText.Length > 0)
                tUnsplitList.Add(tRawText);

            foreach (string tUnsplitString in tUnsplitList)
            {
                //Sanity check to make sure that this is a site
                if (!Regex.IsMatch(tUnsplitString, "[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}"))
                    break;

                //Sanity check to make sure the information for the site is complete
                List<string> tSplitString = tUnsplitString.Split(new string[] { "\t" }, StringSplitOptions.None).ToList();
                if (tSplitString.Count != 6)
                    break;

                //XBM-897	Cosmic Signature	Magnetometric   Crumbling Angel Antiquated Outpost	100.00%	30.38 AU
                Dictionary<string, string> tSite = new Dictionary<string, string>()
                    {   
                        { "SiteCode" , tSplitString[0] } ,
                        { "SiteScanType" , tSplitString[1] },
                        { "SiteGroup" , tSplitString[2] },
                        { "SiteName" , tSplitString[3] },
                        { "SignalStrength" , tSplitString[4] },
                        { "SiteDistance" , tSplitString[5] }
                    };

                string tSiteCheckboxName = tSplitString[0] + " | " + (tSplitString[3].Length != 0 ? tSplitString[3] : "Unknown Name");

                //Cosmic Anomalies always have 100% result strength and can be ignored
                if (tSplitString[1] == "Cosmic Signature" && KnownSiteDict.Keys.Contains(tSplitString[0]) != true)
                {   
                    UnknownSiteDict.Add(tSplitString[0], tSite);
                    UnknownSitesChecklist.Items.Add(tSiteCheckboxName, false);
                }
                else
                {
                    KnownSitesChecklist.Items.Add(tSiteCheckboxName, false);
                }
            }
        }

        private void Mark_Known_click(object sender, EventArgs e)
        {
            //Have to copy out the items into a new array for iteration since we're modifying the original
            string[] tCheckedItemArray = new string[UnknownSitesChecklist.CheckedItems.Count];
            UnknownSitesChecklist.CheckedItems.CopyTo(tCheckedItemArray, 0);
            
            foreach (var tCheckedItem in tCheckedItemArray)
            {
                string tCheckedItemText = tCheckedItem.ToString();
                string tSiteCode = tCheckedItemText.Split('|')[0].TrimEnd();
                if (!KnownSiteDict.Keys.Contains(tSiteCode))
                {
                    KnownSiteDict.Add(tSiteCode, UnknownSiteDict[tSiteCode]);
                }

                KnownSitesChecklist.Items.Add(tCheckedItem);

                UnknownSiteDict.Remove(tSiteCode);
                UnknownSitesChecklist.Items.Remove(tCheckedItem);
            }
        }

        private void Clear_Sites_click(object sender, EventArgs e)
        {
            KnownSiteDict.Clear();
            UnknownSiteDict.Clear();

            KnownSitesChecklist.Items.Clear();
            UnknownSitesChecklist.Items.Clear();
        }

        //Fix for not being able to ctrl-a all text in a text box control
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 1) //1 is Ctrl A
            {
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
        }
    }
}
